package com.chenxingnet.service.Impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.chenxingnet.dao.DepartmentMapper;
import com.chenxingnet.dao.RemindMapper;
import com.chenxingnet.dao.WaybillsMapper;
import com.chenxingnet.dto.Department;
import com.chenxingnet.dto.Remind;
import com.chenxingnet.dto.Waybills;
import com.chenxingnet.dto.vo.WaybillVo;
import com.chenxingnet.service.IRemindService;
import com.chenxingnet.service.WaybillsService;
import com.chenxingnet.tools.MobilePage;
import com.chenxingnet.tools.Page;
@Service("waybillsService")
public class WaybillsServiceImpl implements WaybillsService{
	
	@Resource
	private WaybillsMapper waybillsMapper;
	@Resource
	private DepartmentMapper departmentMapper;
	@Resource
	private RemindMapper remindMapper;
	@Resource
	private IRemindService  iRemindService;
	public List<Map> findByProperty(Map params){
		return waybillsMapper.findByProperty(params);
	}
	
	public Long countByProperty(Map params){
		return waybillsMapper.countByProperty(params);
	}
	//推送信息
	public int addAppWaybill(Waybills waybills) {
		int insertWay = waybillsMapper.insertSelective(waybills);
		/*int insertWay = waybillsMapper.insertSelectiveMy(waybills);*/
		//新增运单生成提醒内容
		/*iRemindService.getuiRemindByBills(waybills);*/
		return insertWay;
	}
	
	public int addWaybill(Waybills waybills) {
		int insertWay = waybillsMapper.insertSelective(waybills);
		/*int insertWay = waybillsMapper.insertSelectiveMy(waybills);*/
		/*int bills = iRemindService.generateRemindByBills(waybills);*/
		return insertWay;
	}

	public void updateWaybill(Waybills waybills) {
		waybillsMapper.updateByPrimaryKeySelective(waybills);
		
	}

	public void deleteWaybill(Integer wid) {
		/*waybillsMapper.deleteByPrimaryKey(wid);*/
		int byId = waybillsMapper.updateStartById(wid);
		System.out.println("已修改wid:"+wid+"数据状态");
	}

	public Waybills selectById(Integer wid) {
		
		return waybillsMapper.selectByPrimaryKey(wid);
	}

	public List<WaybillVo> selectByDeptid(Integer deptid,HttpServletRequest request, Model model) {
		Department department = departmentMapper.selectByPrimaryKey(deptid);
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		int totalCount = 0;
		List<WaybillVo> wayVoList = new ArrayList<WaybillVo>();
		List<Waybills> list = null;
		String souVin = request.getParameter("souVin");
		if(souVin != null){
			List<Waybills> byVin = waybillsMapper.showByVin(souVin, deptid);
			for(int i=0;i<byVin.size();i++){
				WaybillVo vo = new WaybillVo();
				vo.setWayid(byVin.get(i).getWid());
				vo.setActuate(byVin.get(i).getWactuate());
				vo.setComCity(byVin.get(i).getWcommence());
				vo.setBournCity(byVin.get(i).getWbourn());
				vo.setVehicle(byVin.get(i).getWvehicle());
				vo.setVin(byVin.get(i).getWvin());
				vo.setComDate(byVin.get(i).getWdeparturetime());
				vo.setYujiDate(byVin.get(i).getWestimatedtime());
				vo.setNearDate(byVin.get(i).getWneartime());
				vo.setInsuranceDate(byVin.get(i).getWinsurancetime());
				vo.setTransport(byVin.get(i).getWtransport());
				vo.setFactoryMoney(byVin.get(i).getWfactory());
				vo.setPilotMoney(byVin.get(i).getWpilot());
				vo.setInsuranceMoney(byVin.get(i).getWcommercial().add(byVin.get(i).getWcoerce()));
				vo.setCommercialMoney(byVin.get(i).getWcommercial());
				vo.setCoerceMoney(byVin.get(i).getWcoerce());
				vo.setAmountMoney(byVin.get(i).getWamount());
				vo.setCreatDate(byVin.get(i).getWcreationtime());
				vo.setRemark(byVin.get(i).getWremark());
				vo.setStateAcc(byVin.get(i).getWstate());
				vo.setOutTime(byVin.get(i).getWtimeout());
				vo.setDelivery(byVin.get(i).getWdelivery());
				vo.setProfitMoney(byVin.get(i).getWprofit());
				
				vo.setDeptName(department.getDeptname());
				vo.setDeptid(department.getDeptid());
				//指令号
				vo.setwInstruct(byVin.get(i).getWinstruct());
			
				wayVoList.add(vo);
			}
			
		}else{
			totalCount = (int)waybillsMapper.getCount(deptid);
			if(pageNow != null){
				page = new Page(totalCount, Integer.valueOf(pageNow));
				list = waybillsMapper.selectByDeptid(deptid,page.getStartPos(), page.getPageSize());
				for(int i=0;i<list.size();i++){
					WaybillVo vo = new WaybillVo();
					vo.setWayid(list.get(i).getWid());
					vo.setActuate(list.get(i).getWactuate());
					vo.setComCity(list.get(i).getWcommence());
					vo.setBournCity(list.get(i).getWbourn());
					vo.setVehicle(list.get(i).getWvehicle());
					vo.setVin(list.get(i).getWvin());
					vo.setComDate(list.get(i).getWdeparturetime());
					vo.setYujiDate(list.get(i).getWestimatedtime());
					vo.setNearDate(list.get(i).getWneartime());
					vo.setInsuranceDate(list.get(i).getWinsurancetime());
					vo.setTransport(list.get(i).getWtransport());
					vo.setFactoryMoney(list.get(i).getWfactory());
					vo.setPilotMoney(list.get(i).getWpilot());
					vo.setInsuranceMoney(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
					vo.setCommercialMoney(list.get(i).getWcommercial());
					vo.setCoerceMoney(list.get(i).getWcoerce());
					vo.setAmountMoney(list.get(i).getWamount());
					vo.setCreatDate(list.get(i).getWcreationtime());
					vo.setRemark(list.get(i).getWremark());
					vo.setStateAcc(list.get(i).getWstate());
					vo.setOutTime(list.get(i).getWtimeout());
					vo.setDelivery(list.get(i).getWdelivery());
					vo.setProfitMoney(list.get(i).getWprofit());
					
					vo.setDeptName(department.getDeptname());
					vo.setDeptid(department.getDeptid());
				
					//指令号
					vo.setwInstruct(list.get(i).getWinstruct());
					
					wayVoList.add(vo);
				}
			}else{
				page = new Page(totalCount, 1);
				list = waybillsMapper.selectByDeptid(deptid,page.getStartPos(), page.getPageSize());
				for(int i=0;i<list.size();i++){
					WaybillVo vo = new WaybillVo();
					vo.setWayid(list.get(i).getWid());
					vo.setActuate(list.get(i).getWactuate());
					vo.setComCity(list.get(i).getWcommence());
					vo.setBournCity(list.get(i).getWbourn());
					vo.setVehicle(list.get(i).getWvehicle());
					vo.setVin(list.get(i).getWvin());
					vo.setActuate(list.get(i).getWactuate());
					vo.setComDate(list.get(i).getWdeparturetime());
					vo.setYujiDate(list.get(i).getWestimatedtime());
					vo.setNearDate(list.get(i).getWneartime());
					vo.setInsuranceDate(list.get(i).getWinsurancetime());
					vo.setTransport(list.get(i).getWtransport());
					vo.setFactoryMoney(list.get(i).getWfactory());
					vo.setPilotMoney(list.get(i).getWpilot());
					//保险=强险+商业险
					vo.setInsuranceMoney(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
					vo.setCommercialMoney(list.get(i).getWcommercial());
					vo.setCoerceMoney(list.get(i).getWcoerce());
					vo.setAmountMoney(list.get(i).getWamount());
					vo.setCreatDate(list.get(i).getWcreationtime());
					vo.setRemark(list.get(i).getWremark());
					vo.setStateAcc(list.get(i).getWstate());
					vo.setOutTime(list.get(i).getWtimeout());
					vo.setDelivery(list.get(i).getWdelivery());
					vo.setProfitMoney(list.get(i).getWprofit());
					
					vo.setDeptName(department.getDeptname());
					vo.setDeptid(department.getDeptid());
				
					//指令号
					vo.setwInstruct(list.get(i).getWinstruct());
					
					wayVoList.add(vo);
				}
			}
			
			model.addAttribute("page", page);
		}
		
		return wayVoList;
	}
	/**
	 * 移动端按照部门分页查询
	 */
	public List<WaybillVo> mobSelectByDeptid(Integer deptid,HttpServletRequest request, Model model) {
		Department department = departmentMapper.selectByPrimaryKey(deptid);
		String pageNow = request.getParameter("pageNow");
		MobilePage mobilePage = null;
		List<WaybillVo> wayVoList = new ArrayList<WaybillVo>();
		int totalCount = (int)waybillsMapper.getCount(deptid);
		if(pageNow != null){
			mobilePage = new MobilePage(totalCount, Integer.valueOf(pageNow));
			List<Waybills> list = waybillsMapper.selectByDeptid(deptid,mobilePage.getStartPos(), mobilePage.getPageSize());
			for(int i=0;i<list.size();i++){
				WaybillVo vo = new WaybillVo();
				vo.setWayid(list.get(i).getWid());
				vo.setActuate(list.get(i).getWactuate());
				vo.setComCity(list.get(i).getWcommence());
				vo.setBournCity(list.get(i).getWbourn());
				vo.setVehicle(list.get(i).getWvehicle());
				vo.setVin(list.get(i).getWvin());
				vo.setComDate(list.get(i).getWdeparturetime());
				vo.setYujiDate(list.get(i).getWestimatedtime());
				vo.setNearDate(list.get(i).getWneartime());
				vo.setInsuranceDate(list.get(i).getWinsurancetime());
				vo.setTransport(list.get(i).getWtransport());
				vo.setFactoryMoney(list.get(i).getWfactory());
				vo.setPilotMoney(list.get(i).getWpilot());
				vo.setInsuranceMoney(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
				vo.setCommercialMoney(list.get(i).getWcommercial());
				vo.setCoerceMoney(list.get(i).getWcoerce());
				vo.setAmountMoney(list.get(i).getWamount());
				vo.setCreatDate(list.get(i).getWcreationtime());
				vo.setRemark(list.get(i).getWremark());
				vo.setStateAcc(list.get(i).getWstate());
				vo.setOutTime(list.get(i).getWtimeout());
				vo.setDelivery(list.get(i).getWdelivery());
				vo.setProfitMoney(list.get(i).getWprofit());
				
				vo.setDeptName(department.getDeptname());
				vo.setDeptid(department.getDeptid());
			
				wayVoList.add(vo);
			}
		}else{
			mobilePage = new MobilePage(totalCount, 1);
			List<Waybills> list = waybillsMapper.selectByDeptid(deptid,mobilePage.getStartPos(), mobilePage.getPageSize());
			for(int i=0;i<list.size();i++){
				WaybillVo vo = new WaybillVo();
				vo.setWayid(list.get(i).getWid());
				vo.setActuate(list.get(i).getWactuate());
				vo.setComCity(list.get(i).getWcommence());
				vo.setBournCity(list.get(i).getWbourn());
				vo.setVehicle(list.get(i).getWvehicle());
				vo.setVin(list.get(i).getWvin());
				vo.setActuate(list.get(i).getWactuate());
				vo.setComDate(list.get(i).getWdeparturetime());
				vo.setYujiDate(list.get(i).getWestimatedtime());
				vo.setNearDate(list.get(i).getWneartime());
				vo.setInsuranceDate(list.get(i).getWinsurancetime());
				vo.setTransport(list.get(i).getWtransport());
				vo.setFactoryMoney(list.get(i).getWfactory());
				vo.setPilotMoney(list.get(i).getWpilot());
				//保险=强险+商业险
				vo.setInsuranceMoney(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
				vo.setCommercialMoney(list.get(i).getWcommercial());
				vo.setCoerceMoney(list.get(i).getWcoerce());
				vo.setAmountMoney(list.get(i).getWamount());
				vo.setCreatDate(list.get(i).getWcreationtime());
				vo.setRemark(list.get(i).getWremark());
				vo.setStateAcc(list.get(i).getWstate());
				vo.setOutTime(list.get(i).getWtimeout());
				vo.setDelivery(list.get(i).getWdelivery());
				vo.setProfitMoney(list.get(i).getWprofit());
				
				vo.setDeptName(department.getDeptname());
				vo.setDeptid(department.getDeptid());
			
				wayVoList.add(vo);
			}
		}
		
		request.setAttribute("mobilePage", mobilePage);
		return wayVoList;
	}

	public List<WaybillVo> selectAllCount(HttpServletRequest request, Model model) {
		String pageNow = request.getParameter("pageNow");
		Page pageAll = null;
		List<WaybillVo> wayVoList = new ArrayList<WaybillVo>();
		int totalCount = (int)waybillsMapper.getCountAll();
		if(pageNow != null){
			pageAll = new Page(totalCount, Integer.valueOf(pageNow));
			
			List<Waybills> list = waybillsMapper.selectAllCount(pageAll.getStartPos(), pageAll.getPageSize());
			
			for (int i = 0; i < list.size(); i++) {
				WaybillVo wayVo = new WaybillVo();
				Integer wid = list.get(i).getWid();
				wayVo.setWayid(list.get(i).getWid());
				wayVo.setDeptid(list.get(i).getDeptid());
				wayVo.setActuate(list.get(i).getWactuate());
				wayVo.setComCity(list.get(i).getWcommence());
				wayVo.setBournCity(list.get(i).getWbourn());
				wayVo.setVehicle(list.get(i).getWvehicle());
				wayVo.setVin(list.get(i).getWvin());
				wayVo.setActuate(list.get(i).getWactuate());
				wayVo.setComDate(list.get(i).getWdeparturetime());
				wayVo.setYujiDate(list.get(i).getWestimatedtime());
				wayVo.setNearDate(list.get(i).getWneartime());
				wayVo.setInsuranceDate(list.get(i).getWinsurancetime());
				wayVo.setTransport(list.get(i).getWtransport());
				wayVo.setFactoryMoney(list.get(i).getWfactory());
				wayVo.setPilotMoney(list.get(i).getWpilot());
				//保险=商业险+强险
				wayVo.setInsuranceMoney(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
				wayVo.setCommercialMoney(list.get(i).getWcommercial());
				wayVo.setCoerceMoney(list.get(i).getWcoerce());
				wayVo.setAmountMoney(list.get(i).getWamount());
				wayVo.setCreatDate(list.get(i).getWcreationtime());
				wayVo.setRemark(list.get(i).getWremark());
				wayVo.setStateAcc(list.get(i).getWstate());
				wayVo.setOutTime(list.get(i).getWtimeout());
				wayVo.setDelivery(list.get(i).getWdelivery());
				wayVo.setProfitMoney(list.get(i).getWprofit());
				
				Department dept = departmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
				wayVo.setDeptName(dept.getDeptname());
				wayVoList.add(wayVo);
			}
		}else{
			pageAll = new Page(totalCount, 1);
			List<Waybills> list = waybillsMapper.selectAllCount(pageAll.getStartPos(), pageAll.getPageSize());
			
			for (int i = 0; i < list.size(); i++) {
				WaybillVo wayVo = new WaybillVo();
				Integer wid = list.get(i).getWid();
				wayVo.setWayid(list.get(i).getWid());
				wayVo.setDeptid(list.get(i).getDeptid());
				wayVo.setActuate(list.get(i).getWactuate());
				wayVo.setComCity(list.get(i).getWcommence());
				wayVo.setBournCity(list.get(i).getWbourn());
				wayVo.setVehicle(list.get(i).getWvehicle());
				wayVo.setVin(list.get(i).getWvin());
				wayVo.setActuate(list.get(i).getWactuate());
				wayVo.setComDate(list.get(i).getWdeparturetime());
				wayVo.setYujiDate(list.get(i).getWestimatedtime());
				wayVo.setNearDate(list.get(i).getWneartime());
				wayVo.setInsuranceDate(list.get(i).getWinsurancetime());
				wayVo.setTransport(list.get(i).getWtransport());
				wayVo.setFactoryMoney(list.get(i).getWfactory());
				wayVo.setPilotMoney(list.get(i).getWpilot());
				wayVo.setInsuranceMoney(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
				wayVo.setCommercialMoney(list.get(i).getWcommercial());
				wayVo.setCoerceMoney(list.get(i).getWcoerce());
				wayVo.setAmountMoney(list.get(i).getWamount());
				wayVo.setCreatDate(list.get(i).getWcreationtime());
				wayVo.setRemark(list.get(i).getWremark());
				wayVo.setStateAcc(list.get(i).getWstate());
				wayVo.setOutTime(list.get(i).getWtimeout());
				wayVo.setDelivery(list.get(i).getWdelivery());
				wayVo.setProfitMoney(list.get(i).getWprofit());
				
				Department dept = departmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
				wayVo.setDeptName(dept.getDeptname());
				wayVoList.add(wayVo);
			}
		}
		model.addAttribute("pageAll", pageAll);
		return wayVoList;
	}

	public List<String> selectAllByChexing() {
		return waybillsMapper.selectAllByChexing();
	}

	public List<WaybillVo> selectAll() {
		
		List<WaybillVo> wayVoList = new ArrayList<WaybillVo>();
		List<Waybills> list = waybillsMapper.selectAll();
		
		for (int i = 0; i < list.size(); i++) {
			WaybillVo wayVo = new WaybillVo();
			Integer wid = list.get(i).getWid();
			wayVo.setWayid(list.get(i).getWid());
			wayVo.setDeptid(list.get(i).getDeptid());
			wayVo.setActuate(list.get(i).getWactuate());
			wayVo.setComCity(list.get(i).getWcommence());
			wayVo.setBournCity(list.get(i).getWbourn());
			wayVo.setVehicle(list.get(i).getWvehicle());
			wayVo.setVin(list.get(i).getWvin());
			wayVo.setActuate(list.get(i).getWactuate());
			wayVo.setComDate(list.get(i).getWdeparturetime());
			wayVo.setYujiDate(list.get(i).getWestimatedtime());
			wayVo.setNearDate(list.get(i).getWneartime());
			wayVo.setInsuranceDate(list.get(i).getWinsurancetime());
			wayVo.setTransport(list.get(i).getWtransport());
			wayVo.setFactoryMoney(list.get(i).getWfactory());
			wayVo.setPilotMoney(list.get(i).getWpilot());
			wayVo.setInsuranceMoney(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
			wayVo.setCommercialMoney(list.get(i).getWcommercial());
			wayVo.setCoerceMoney(list.get(i).getWcoerce());
			wayVo.setAmountMoney(list.get(i).getWamount());
			wayVo.setCreatDate(list.get(i).getWcreationtime());
			wayVo.setRemark(list.get(i).getWremark());
			wayVo.setStateAcc(list.get(i).getWstate());
			wayVo.setOutTime(list.get(i).getWtimeout());
			wayVo.setDelivery(list.get(i).getWdelivery());
			wayVo.setProfitMoney(list.get(i).getWprofit());
			
			Department dept = departmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
			wayVo.setDeptName(dept.getDeptname());
			wayVoList.add(wayVo);
		}
		return wayVoList;
	}

	public int setWayBillImgAndMarkUser(int parseInt, String filename) {
		Waybills   waybills=new  Waybills();
		waybills.setWid(parseInt);
		waybills.setWpicture(filename);
		waybills.setWtimeout("已结算");
		/*waybills.setWdept(userid);*///存入登记人的用户id
		return waybillsMapper.updateByPrimaryKeySelective(waybills);
	}

	public int updateSetSongda(String wayidSingle) {
		//如果已生成提醒信息，已送达修改超时信息rsend字段为0
		Remind remind = remindMapper.getRemByWid(Integer.valueOf(wayidSingle));
		if(remind != null){
			Remind rem = new Remind();
			rem.setWid(remind.getWid());
			rem.setRsend("0");
			int aa = remindMapper.updateByWid(rem);
			System.out.println("已修改"+aa+"条rsend字段");
		}
		
		return waybillsMapper.updateSetSongda(wayidSingle);
	}

	public List<WaybillVo> selectWayidSingle(int parseInt) {

		List<WaybillVo> wayVoList = new ArrayList<WaybillVo>();
		Waybills   ay=	waybillsMapper.selectByPrimaryKey(parseInt);
		List<Waybills> list =new  ArrayList<Waybills>();
		list.add(ay);
		for (int i = 0; i < list.size(); i++) {
			WaybillVo wayVo = new WaybillVo();
			Integer wid = list.get(i).getWid();
			wayVo.setWayid(list.get(i).getWid());
			wayVo.setDeptid(list.get(i).getDeptid());
			wayVo.setActuate(list.get(i).getWactuate());
			wayVo.setComCity(list.get(i).getWcommence());
			wayVo.setBournCity(list.get(i).getWbourn());
			wayVo.setVehicle(list.get(i).getWvehicle());
			wayVo.setVin(list.get(i).getWvin());
			wayVo.setActuate(list.get(i).getWactuate());
			wayVo.setComDate(list.get(i).getWdeparturetime());
			wayVo.setYujiDate(list.get(i).getWestimatedtime());
			wayVo.setNearDate(list.get(i).getWneartime());
			wayVo.setInsuranceDate(list.get(i).getWinsurancetime());
			wayVo.setTransport(list.get(i).getWtransport());
			wayVo.setFactoryMoney(list.get(i).getWfactory());
			wayVo.setPilotMoney(list.get(i).getWpilot());
			wayVo.setInsuranceMoney(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
			wayVo.setCommercialMoney(list.get(i).getWcommercial());
			wayVo.setCoerceMoney(list.get(i).getWcoerce());
			wayVo.setAmountMoney(list.get(i).getWamount());
			wayVo.setCreatDate(list.get(i).getWcreationtime());
			wayVo.setRemark(list.get(i).getWremark());
			wayVo.setStateAcc(list.get(i).getWstate());
			wayVo.setOutTime(list.get(i).getWtimeout());
			wayVo.setDelivery(list.get(i).getWdelivery());
			wayVo.setProfitMoney(list.get(i).getWprofit());
			
			Department dept = departmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
			wayVo.setDeptName(dept.getDeptname());
			wayVoList.add(wayVo);
		}
		return wayVoList;
	}

	public int updateByShiSongda(String wayidSingle) {
		
		return waybillsMapper.updateByShiSongda(wayidSingle);
	}

	public int insertSelective(Waybills record) {
		int insertWay = waybillsMapper.insertSelective(record);
		int bills = iRemindService.generateRemindByBills(record);
		System.out.println(bills);
		return insertWay;
	}

	/**
	 * 未送达的数据
	 * 值相减
	 */
	public int getCountByWS(Integer deptid) {
		int count = (int)waybillsMapper.getCount(deptid);
		int songda = (int)waybillsMapper.getCountByS(deptid);
		int wscount ;
		wscount = count - songda;
		return wscount;
	}
	/**
	 * 已送达的数据
	 */
	public int getCountByS(Integer deptid) {
		
		return (int)waybillsMapper.getCountByS(deptid);
	}
	/**
	 * 超时的数据
	 */
	public int getCountByCHS(Integer deptid) {
	
		return (int)waybillsMapper.getCountByCHS(deptid);
	}
	/**
	 * 发生事故的数据
	 */
	public int getCountbYSG(Integer deptid) {
		
		return (int)waybillsMapper.getCountbYSG(deptid);
	}

	public int updateChangshangjiesuanshi(int parseInt) {
		return waybillsMapper.updateChangshangjiesuanshi(parseInt);
	}

	public Waybills showRemBywId(Integer wid) {
		
		return waybillsMapper.showRemById(wid);
	}

	
	



}
